package com.qf.controller;

import com.qf.entity.Bookxx;
import com.qf.service.BookService;
import com.qf.service.UserAndBookService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 阿亘
 * @date 2023/07/14
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;
    @Resource
    private UserAndBookService userAndBookService;

    /**
     * 书籍列表
     *
     * @param model 模型
     * @return {@link String}
     */
    @RequestMapping("/listBooks")
    public String listBooks(Model model) {
        List<Bookxx> books = bookService.listBooks();
        model.addAttribute("books", books);
        return "listBooks";
    }

    /**
     * 找到书名字
     *
     * @param bookname bookname
     * @return {@link Bookxx}
     */
    @RequestMapping("/findBookByName")
    public Bookxx findBookByName(String bookname) {
        return bookService.findBookByName(bookname);
    }

    @RequestMapping("/toAddBook")
    public String toAddBook() {
        return "addBook";
    }

    /**
     * 添加书
     *
     * @param book  书
     * @param model 模型
     * @return {@link String}
     */
    @PostMapping("/addBook")
    @Transactional
    public String addBook(Bookxx book, Model model) {
        // 查看是否存在同名书籍(不允许同名书籍存在)
        Bookxx check = bookService.findBookByName(book.getBookname());
        if (check != null) {
            model.addAttribute("msg", "添加失败, 已存在同名书籍");
        } else {
            int affected = bookService.addBook(book);
            if (affected > 0) {
                model.addAttribute("msg", "添加成功, 新书目的id为 " + book.getBookid());
            } else {
                model.addAttribute("msg", "添加失败");
            }
        }
        return "information";
    }


    /**
     * 删除书
     *
     * @param bookid bookid
     * @param model  模型
     * @return {@link String}
     */
    @GetMapping("/delBook")
    @Transactional
    public String deleteBook(@RequestParam("bookid") Integer bookid, Model model){
        int affected = bookService.deleteBool(bookid);
        if (affected > 0){
            model.addAttribute("msg", "成功删除id为 " + bookid + " 的书：");
        } else {
            model.addAttribute("msg", "删除失败");
        }
        return "information";
    }

    /**
     * 更新书
     *
     * @param model  模型
     * @param bookid bookid
     * @return {@link String}
     */
    @RequestMapping("/toUpdateBook/{bookid}")
    public String toUpdateBook(Model model, @PathVariable Integer bookid){
        model.addAttribute("bookid", bookid);
        return "updateBook";
    }

    /**
     * 更新书目信息
     * @param book 需要更新的书的新信息
     * @param model
     * @return
     */
    @PostMapping("/updateBook")
    @Transactional
    public String updateBook(Bookxx book, Model model){
        int affected = bookService.updateBook(book);
        if (affected > 0){
            model.addAttribute("msg", "更新成功");
        } else {
            model.addAttribute("msg", "更新失败");
        }
        return "information";
    }

    /**
     * 借书
     *
     * @param request 请求
     * @return {@link Map}<{@link String}, {@link String}>
     */
    @RequestMapping("/borrowBook")
    @ResponseBody
    @Transactional
    public Map<String, String> borrowBook(@RequestBody Map<String, String> request){
        int bookid = Integer.parseInt(request.get("bookid"));
        int userid = Integer.parseInt(request.get("userid"));
        Map<String, String> response = new HashMap<>(1);
        // 判断是否存在借过此书的记录(不允许借同一本)
        Integer ifBorrow = userAndBookService.checkIfBorrow(bookid, userid);
        if (ifBorrow != null){
            response.put("info", "借书失败, 你已借过此书");
        } else {
            bookService.borrowBook(bookid);
            //int i = 10/0;
            userAndBookService.borrowBook(bookid, userid);
            response.put("info", "借书成功");
        }
        return response;
    }

    /**
     * 返回书
     *
     * @param request 请求
     * @return {@link Map}<{@link String}, {@link String}>
     */
    @RequestMapping("/returnBook")
    @ResponseBody
    @Transactional
    public Map<String, String> returnBook(@RequestBody Map<String, String> request){
        int bookid = Integer.parseInt(request.get("bookid"));
        int userid = Integer.parseInt(request.get("userid"));
        Map<String, String> response = new HashMap<>(1);
        userAndBookService.retuenBook(bookid, userid);
        bookService.returnBook(bookid);
        response.put("info", "还书成功");
        return response;
    }
}
